package pers.liyan.shi.computermanager.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.liyan.shi.computermanager.util.SystemUtil;

/**
 * @author ShiLY
 * @version 1.0
 * @date 2020/2/10 23:13
 */
@Slf4j
@RestController
@RequestMapping(value = "/manager", produces = MediaType.TEXT_PLAIN_VALUE)
public class ManagerController {

    @Value("${computer.shutdown.seconds}")
    private int shutdownSeconds;

    @Value("${computer.restart.seconds}")
    private int restartSeconds;

    @Autowired
    private SystemUtil systemUtil;

    @PostMapping(value = "/echo")
    public String echo(@RequestParam String msg) {
        log.info("simple call echo: {}", msg);
        return msg;
    }

    @PostMapping("/shutdown")
    public String shutdown() {
        log.info("simple call shutdown");
        return systemUtil.execCmdRtString("shutdown -s -t " + shutdownSeconds);

    }

    @PostMapping("/cancel")
    public String cancelShutdown() {
        log.info("simple call cancel");
        return systemUtil.execCmdRtString("shutdown -a");
    }

    @PostMapping("/restart")
    public String restart() {
        log.info("simple call restart");
        return systemUtil.execCmdRtString("shutdown -r -t " + restartSeconds);
    }

}
